/*
	----------------------------------------------------------
*/
var wp;

//静态-类型下拉选框
var sc = [
	{type:0, data:["","0","1","2"],str:["","增","删","改"]},
]; 	/* 调用：wp.fn.toSource(0,"",sc) */
//静态-按钮区
var button_group = {};

$(function () {
	//自定义对象
	wp = {
		fn : new Object()
	};
	//自定义方法
	wp.fn.toSource = function(type,num,sc){
		var d = sc[type];
		for(var i in d.data){
			var r = d.data[i];
			if(xyd.fn.geteTypeof(r)==xyd.fn.geteTypeof(num)){
				if(r==num){return d.str[i];}
			}else{
				return undefined; // 类型错误
			}
		}
		return undefined; // 数据错误
	};

	//0.配置相关参数
	var tableId = "#tb_departments",		//表格ID
		selectTrue = false,					//是否单选,true为单选，false为多选
		ajaxUrl = xyd.api.path+'/logoperate/list',
		ajaxType = 'get',					//ajax方式,get,post
		ajaxTableColumns = [{				//表格头信息
			checkbox: true
		},{
			field: 'operId', title: '操作人'
		},{
			field: 'operType', title: '操作类型'
		},{
			field: 'moduleName', title: '模块名称'
		},{
			field: 'actionName', title: '功能名称'
		},{
			field: 'detail', title: '详细说明'
		},{
			field: 'crtDatetime', title: '创建时间'
		},{
			field: 'edit', title: '操作'
		}]
		; 

	//1.初始化Table
	var oTable = new TableInit(tableId,selectTrue);

	//2.配置查询参数
	oTable.queryParams = function (params) {
		var temp = {
			// 查询条件，传实体类属性
			operId : $("#txt_search_1").val(),
			operType : $("#txt_search_2").val(),
			moduleName : $("#txt_search_3").val(),

			// pageSize,pageNumber不允许更改，但必须存在，分页用
			pageSize: params.pageSize,
			pageNumber:params.pageNumber,
			// 时间戳（可不传）
			timestamp: new Date().getTime()
		};
		return temp;
	};
	
	//3.提交查询
	oTable.ajaxSubmit({
		url : ajaxUrl,
		type : ajaxType,
		columns : ajaxTableColumns,
		uniqueId : "id"  //uuid -> id
	});
	
	//数据加载完毕后增加内部按钮
	oTable.onLoadSuccess(function(){
		var data = oTable.getData();
		for(var i=0;i<data.length;i++){
			oTable.updateRow(i,{
				"edit":"<div class=\"hidden-sm hidden-xs btn-group\"> \
				<button onclick=\"button_group._search('"+data[i].id+"')\" class=\"btn btn-xs btn-success\">\
					<span aria-hidden=\"true\" class=\"glyphicon glyphicon-search\"></span>\
				</button>\
				<button onclick=\"button_group._edit('"+data[i].id+"')\" class=\"btn btn-xs btn-info\">\
					<i class=\"ace-icon fa fa-pencil bigger-120\"></i>\
				</button>\
				<button onclick=\"button_group._removeId('"+data[i].id+"')\" class=\"btn btn-xs btn-danger btn-user-delete\">\
					<i class=\"ace-icon fa fa-trash-o bigger-120\"></i>\
				</button>\
			</div>"
			});
			// 列数据转换
			oTable.updateRow(i,{"operType":wp.fn.toSource(0,data[i].operType+"",sc)});
		}
	});
	
	/*
		----------------------------------------------------------
		按钮功能区
		----------------------------------------------------------
	*/
	
	var toolbar = $("#toolbar");
	var btn_add = toolbar.find("#btn_add");
	var btn_removeSel = toolbar.find("#btn_delete");
	// var btn_httpDown = toolbar.find("#btn_httpDown");
	// var btn_httpUpload = toolbar.find("#btn_httpUpload");
	
	//按钮-查询数据
	$("#btn_query").click(function(){
		oTable.search();
	});
	
	//按钮-增加本地数据

	//按钮-删除选中数据
	btn_removeSel.click(function(){
		var d = oTable.selectId();
		if(d.length<1){
			layer.msg('请选择数据行');
			return;
		}
		layer.confirm('您确认删除选中的全部数据吗?删除后不可恢复！', {title:'提示'}, function(index){
			var ids = "";
			for(var i in d){
				ids += d[i].id+",";
			}
			ids = ids.substr(0,ids.length-1);
			// 表单提交
			$.ajax({
				url: xyd.api.path+'/logoperate/deletes/'+ids,
				type: 'POST',
				dataType: 'JSON'
			}).done(function ( data ) {
				layer.msg(data.statusCode);
				if(data.success){
					layer.close(i);
					oTable.search();
				}
			});
			// 表单提交 结束//
		});
	});
	
	//内部按钮-查看
	button_group._search = function(id){
		var data = oTable.getId(id);
		$.get(xyd.pageBean.jm+'logoperate/add.html', {}, function(str){
			// 弹出窗体
			layer.open({
				type: 1,
				area: ['700px', 'auto'],
				title: '查看详情',
				content: str,
				btn: ['关闭']
			});
			/* 数据回显*/
			var form = $("#form_add");
			var var1 = form.find("input[name='input_1']");
			var var2 = form.find("input[name='input_2']");
			var var3 = form.find("select[name='select_1']");
			var var4 = form.find("input[name='input_4']");
			var var5 = form.find("input[name='input_5']");

			form.find("input").val("").attr("disabled","disabled");
			var1.val(data.operId);
			var2.val(data.moduleName);
			var3.val("").attr("disabled","disabled");
			var3.find("option").each(function(){
				if($(this).text()==data.operType){
					$(this).attr("selected",true);
				}
			});
			var4.val(data.actionName);
			var5.val(data.detail);
			/* 数据回显 结束*/
		});
	}
	//内部按钮-编辑
	button_group._edit = function(id){
		var data = oTable.getId(id);
		$.get(xyd.pageBean.jm+'logoperate/add.html', {}, function(str){
			/* 弹出窗体*/
			layer.open({
				type: 1,
				area: ['700px', 'auto'],
				title: '编辑用户',
				content: str, //注意，如果str是object，那么需要字符拼接。
				btn: ['编辑保存', '取消'],
				yes:function(i){
					layer.confirm('您确认保存这条数据吗?', {title:'提示',btn: ['YES', 'NO']}, function(index){
						var dataList = {
							operId : var1.val(),
							moduleName : var2.val(),
							operType : var3.val(),
							actionName : var4.val(),
							detail : var5.val()
						};
						dataList.id = data.id;
						/* 表单提交*/
						$.ajax({
							url: xyd.api.path+'/logoperate/update',
							data: dataList,
							type: 'POST',
							dataType: 'JSON'
						}).done(function ( data ) {
							layer.msg(data.statusCode);
							if(data.success){
								layer.close(i);
								oTable.search();
							}
						});/* 表单提交 结束*/
					});
				}
			});
			/* 数据回显*/
			var form = $("#form_add");
			var var1 = form.find("input[name='input_1']");
			var var2 = form.find("input[name='input_2']");
			var var3 = form.find("select[name='select_1']");
			var var4 = form.find("input[name='input_4']");
			var var5 = form.find("input[name='input_5']");

			var1.val(data.operId);
			var2.val(data.moduleName);
			var3.find("option").each(function(){
				if($(this).text()==data.operType){
					$(this).attr("selected",true);
				}
			});
			var4.val(data.actionName);
			var5.val(data.detail);
			/* 数据回显 结束*/
		});
	}
	
	//内部按钮-删除
	button_group._removeId = function(id){
		if(!id){
			layer.msg("参数错误");
			return;
		}
		layer.confirm('您确认删除该条数据吗? 删除后不可恢复！', {title:'提示',btn: ['YES', 'NO']}, function(index){
			oTable.removeId(id);
			// 表单提交
			$.ajax({
				url: xyd.api.path+'/logoperate/delete/'+id,
				type: 'POST',
				dataType: 'JSON'
			}).done(function ( data ) {
				layer.msg(data.statusCode);
				if(data.success){
					layer.close(i);
					oTable.search();
				}
			});
			// 表单提交 结束//
		});	
		
	}

});